#include "uaddbookwidget.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
UAddBookWidget::UAddBookWidget(QWidget *parent)
	: QWidget(parent)
	, m_pAdd(new QPushButton(this))
	, m_pClear(new QPushButton(this))

	, m_pTitle(new QLabel(this))
	, m_pAuthor(new QLabel(this))
	, m_pPress(new QLabel(this))
	, m_pIsbn(new QLabel(this))
	, m_pDate(new QLabel(this))
	, m_pImage(new QLabel(this))

	, m_pTitleEdit(new QLineEdit(this))
	, m_pAuthorEdit(new QLineEdit(this))
	, m_pPressEdit(new QLineEdit(this))
	, m_pIsbnEdit(new QLineEdit(this))
	, m_pDateEdit(new QLineEdit(this))
	, m_pImageEdit(new QLineEdit(this))

	, m_pPrompt(new QLabel(this))
{
	setWindowTitle("添加书籍");
	setAttribute(Qt::WA_StyledBackground, true);
	setWindowFlags(Qt::NoDropShadowWindowHint);// 设置为无边框窗口
	setStyleSheet(R"(UVersionManageWidget{background-color:#F5F5F5;border-radius:4px;border:1px solid #D9D8D7;border:none;})");
	resize(680, 500);

	initButton();
	initLabel();
	initLineEdit();
	connect(m_pAdd, &QPushButton::released, this, &UAddBookWidget::onAddButtonReleased);
	connect(m_pClear, &QPushButton::released, this, &UAddBookWidget::onClearButtonReleased);

	connect(UClientServer::Instance(), &UClientServer::alreadyRead, this, &UAddBookWidget::readResponse);
}

UAddBookWidget::~UAddBookWidget()
{}

void UAddBookWidget::readResponse(const OPERATOR type, QByteArray datagram)
{
	{
		switch (type)
		{
		case OPERATOR::ADDBOOK:      ParsePrompt(datagram);			break;
		case OPERATOR::UNKNINFO:     qDebug() << "识别失败";		    break;
		}
	}
}

void UAddBookWidget::ParsePrompt(const QByteArray& datagram)
{
	m_pPrompt->setText("等待服务器添加...");
	QDataStream ds(datagram);
	OPERATOR buffer;
	QByteArray data;
	ds >> buffer >> data;
	m_pPrompt->setText(QString(data));
}

void UAddBookWidget::onAddButtonReleased()
{
	UClientServer::Instance()->addBook(m_pTitleEdit->text(), m_pAuthorEdit->text(), m_pPressEdit->text()
									, m_pIsbnEdit->text(), m_pDateEdit->text(), m_pImageEdit->text());
}

void UAddBookWidget::onClearButtonReleased()
{
	m_pTitleEdit->clear();
	m_pAuthorEdit->clear();
	m_pPressEdit->clear();
	m_pIsbnEdit->clear();
	m_pDateEdit->clear();
	m_pImageEdit->clear();
	m_pPrompt->setText("请输入内容...");
}

void UAddBookWidget::initLineEdit()
{
	QString LineEditStyleSheet = R"(QLineEdit{background-color:#FFFFFF;border:none;font:#000000;font-family:微软雅黑;font-size:14px;})";
	m_pTitleEdit->resize(200, 25);
	m_pTitleEdit->move(115, 50);
	m_pTitleEdit->setStyleSheet(LineEditStyleSheet);

	m_pAuthorEdit->resize(200, 25);
	m_pAuthorEdit->move(115, 85);
	m_pAuthorEdit->setStyleSheet(LineEditStyleSheet);

	m_pPressEdit->resize(200, 25);
	m_pPressEdit->move(115, 120);
	m_pPressEdit->setStyleSheet(LineEditStyleSheet);

	m_pIsbnEdit->resize(200, 25);
	m_pIsbnEdit->move(115, 155);
	m_pIsbnEdit->setStyleSheet(LineEditStyleSheet);

	m_pDateEdit->resize(200, 25);
	m_pDateEdit->move(115, 190);
	m_pDateEdit->setStyleSheet(LineEditStyleSheet);

	m_pImageEdit->resize(200, 25);
	m_pImageEdit->move(115, 225);
	m_pImageEdit->setStyleSheet(LineEditStyleSheet);
}



void UAddBookWidget::initButton()
{
	QString ButtonStyleSheet = R"(QPushButton{background-color:#808080;border:none;font:#000000;font-family:微软雅黑;font-size: 12px;}
									QPushButton:hover{background-color:#E9E9E9;})";
	m_pAdd->resize(60,25);
	m_pAdd->move(400, 330);
	m_pAdd->setText("添加");
	m_pAdd->setStyleSheet(ButtonStyleSheet);

	m_pClear->resize(60, 25);
	m_pClear->move(470, 330);
	m_pClear->setText("清空");
	m_pClear->setStyleSheet(ButtonStyleSheet);
}

void UAddBookWidget::initLabel()
{
	QString LabelStyleSheet = R"(QLabel{border:none;font:#000000;font-family:微软雅黑;font-size:15px;})";
	m_pTitle->resize(60,25);
	m_pTitle->setText("书名");
	m_pTitle->move(50,50);
	m_pTitle->setStyleSheet(LabelStyleSheet);

	m_pAuthor->resize(60, 25);
	m_pAuthor->setText("作者");
	m_pAuthor->move(50, 85);
	m_pAuthor->setStyleSheet(LabelStyleSheet);

	m_pPress->resize(60, 25);
	m_pPress->setText("出版社");
	m_pPress->move(50, 120);
	m_pPress->setStyleSheet(LabelStyleSheet);

	m_pIsbn->resize(60, 25);
	m_pIsbn->setText("ISBN");
	m_pIsbn->move(50, 155);
	m_pIsbn->setStyleSheet(LabelStyleSheet);

	m_pDate->resize(60, 25);
	m_pDate->setText("出版日期");
	m_pDate->move(50, 190);
	m_pDate->setStyleSheet(LabelStyleSheet);

	m_pImage->resize(60, 25);
	m_pImage->setText("图片");
	m_pImage->move(50, 225);
	m_pImage->setStyleSheet(LabelStyleSheet);

	m_pPrompt->move(100, 300);
	m_pPrompt->resize(100, 25);
	m_pPrompt->setText("请输入内容...");
	m_pPrompt->setStyleSheet(LabelStyleSheet);
}